<template>
    <!--采购订单-->
    <div id="PoDetail">
        <v-details :detailData="inputData" :inputConfig="inputConfig" :rules="rules" @pop="setdata"
                   ref="input"></v-details>
    </div>
</template>

<script>
import details from 'commons/Detail'
import baseMixin from "@/mixins/BaseMixin";

export default {
    mixins: [baseMixin],
    props: {
        inputData: {
            type: Object
        }
    },
    name: "PoDetail",
    data() {
        return {
            inputConfig: [
                [
                    {
                        type: "input",
                        label: this.$t('in.pokey1'),
                        disabled: true,
                        props: "pokey",
                    },
                    {
                        type: "select",
                        label: this.$t('common.status'),
                        disabled: true,
                        props: "status",
                        options: this.getOptions("codelkup/POSTATUS")
                    },
                    {
                        type: "select",
                        label: this.$t('common.type'),
                        disabled: false,
                        props: "potype",
                        options: this.getOptions("codelkup/POTYPE")
                    },
                    {
                        type: "date",
                        label: this.$t('in.podate1'),
                        disabled: !this.$store.state.newFlag,
                        props: "podate",
                    },
                ],
                [

                    {
                        type: "input",
                        label: this.$t('in.externpokey'),
                        disabled: true,
                        props: "externpokey"
                    },
                    {
                        type: "table",
                        label: this.$t('in.storerkey'),
                        disabled: !this.$store.state.newFlag,
                        props: "storerkey",
                        tableConfig: [
                            {
                                label: this.$t('config.shipper'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('config.company'),
                                prop: "company",
                                input: "input",
                            },
                            {
                                label: this.$t('config.city'),
                                prop: "city",
                                input: "input",
                            },
                            {
                                label: this.$t('config.provice'),
                                prop: "state",
                                input: "input",
                            },
                            {
                                label: this.$t('config.postcode'),
                                prop: "zip",
                                input: "input",
                            },
                            {
                                label: this.$t('config.contact'),
                                prop: "contact",
                                input: "input",
                            },
                            {
                                label: this.$t('config.phone'),
                                prop: "phone",
                                input: "input",
                            },
                        ],
                        url: `/queryStorer/1/?/${this.$store.state.wms}`
                    },
                    {
                        type: "input",
                        label: this.$t('in.buyersreference'),
                        disabled: false,
                        props: "buyersreference"
                    },
                    {
                        type: "date",
                        label: this.$t('in.expectedreceiptdate'),
                        disabled: !this.$store.state.newFlag,
                        props: "expectedreceiptdate"
                    },
                ],
                [
                    {
                        type: "input",
                        label: this.$t('in.sourcelocation1'),
                        disabled: true,
                        props: "sourcelocation"
                    },
                    {
                        type: "input",
                        label: this.$t('in.sellersreference'),
                        disabled: false,
                        props: "sellersreference"
                    },
                    {
                        type: "input",
                        label: this.$t('in.externalpokey2'),
                        disabled: true,
                        props: "externalpokey2"
                    },
                    {
                        type: "input",
                        label: this.$t('in.otherreference'),
                        disabled: false,
                        props: "otherreference"
                    },
                ],
                [
                    {
                        type: "input",
                        label: this.$t('in.overageoveridepercent'),
                        disabled: false,
                        props: "overageoveridepercent"
                    },
                ]
            ],
            rules: {
                pokey: [
                    {required: true, trigger: 'blur'},
                ],
                storerkey: [
                    {required: true, validator: this.storerkey, trigger: 'blur'},
                ],
                overageoveridepercent: [
                    {required: true, validator: this.overageoveridepercent, trigger: 'blur'},
                ],
                expectedreceiptdate: [
                    {validator: this.expectedreceiptdate, trigger: 'blur'},
                ],
            },
        }
    },
    computed: {},
    methods: {
        expectedreceiptdate(rule, value, callback) {
            if (value === '') {
                callback();
            } else {
                if (value < this.inputData.podate) {
                    this.$message.error(this.$t('msg.msg54'));
                    this.inputData[rule.field] = ""
                    callback(new Error());
                } else {
                    callback()
                }
            }
        },
        overageoveridepercent(rule, value, callback) {
            if (value === '') {
                callback(new Error());
            } else {
                var re = /[^0-9.]/;
                let te = value;
                let flag = re.test(te)
                if (!flag) {
                    if (value < 0) {
                        this.$message.error(this.$t('app.inputNumberPlease'))
                        this.inputData[rule.field] = ""
                        callback(new Error());
                    } else {
                        callback();
                    }
                } else {
                    this.$message.error(this.$t('app.inputNumberPlease'))
                    this.inputData[rule.field] = ""
                    callback(new Error());
                }
            }
        },
        storerkey(rule, value, callback) {
            if (value === '') {
                callback(new Error());
            } else {
                this.$api.get(`/isExists/${1}/${value}/${this.$store.state.wms}`).then(res => {
                    
                        if (parseInt(res.bean) <= 0) {
                            this.$message.error(`不存在的货主:${value}`);
                            this.inputData[rule.field] = ""
                            callback(new Error())
                        } else {
                            callback()
                        }
                }).catch(e => {
                    this.$message.error(e);
                    callback(new Error())
                })
            }
        },
        setdata(data, id, close, listId) {
            this.inputData.storerkey = data.storerkey;
            close(false);
        }
    },
    mounted() {
    },
    watch: {},
    components: {
        'v-details': details
    }
}
</script>

<style scoped>

</style>
